package com.huawei.esdk.uc.u19.north.rest.app.resource.attendee;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.NumberUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.common.ErrInfo;
import com.huawei.esdk.uc.domain.model.bean.AttendeeInfo;
import com.huawei.esdk.uc.loggermgr.LoggerMgr;
import com.huawei.esdk.uc.u19.business.professional.rest.attendee.AttendeeService;
import com.huawei.esdk.uc.u19.north.rest.app.resource.attendee.convert.AttendeeResourceConvert;
import com.huawei.esdk.uc.u19.north.rest.app.resource.ctc.CTCAttendeeResource;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.AttendeeRequest;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.AttendeeResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.DelAtteInfoRest;

@Path("uc/u19/del_attendee")
public class DeleteAttendeeResource
{
    /**
     * 日志对象
     */
    private static final Logger LOGGER = Logger.getLogger(CTCAttendeeResource.class);
    
    /**
     * 与会人源转换对象
     */
    private static final AttendeeResourceConvert ATTENDEE_RESOURCE_CONVERT = new AttendeeResourceConvert();
    
    /**
     * 与会人服务层对象
     */
    private static final AttendeeService ATTENDEE_SERVICE = new AttendeeService();
    
    /**
     * 删除与会者
     * <功能详细描述>
     * @param attendeeRequest
     * @return
     * @see [类、类#方法、类#成员]
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AttendeeResponse delAttendee(AttendeeRequest attendeeRequest)
    {
        AttendeeResponse response = new AttendeeResponse();
        
        if (null == attendeeRequest || null == attendeeRequest.getDelAtteInfoRest()
            || StringUtils.isEmpty(attendeeRequest.getUserId()) || StringUtils.isEmpty(attendeeRequest.getGwIp())
            || StringUtils.isEmpty(attendeeRequest.getConfRoom())
            || StringUtils.isEmpty(attendeeRequest.getDeleteCount()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "delAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(attendeeRequest.getGwIp()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "delAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 删除与会人员的数量必须为数字，并且等于删除的与会者号码的个数
        if (!NumberUtils.isNumber(attendeeRequest.getDeleteCount())
            || Integer.parseInt(attendeeRequest.getDeleteCount()) != attendeeRequest.getDelAtteInfoRest().size())
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "delAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 会议厅号必须为数字, 且不能超int范围
        if (!StringUtils.isInteger(attendeeRequest.getConfRoom()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "delAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        for (DelAtteInfoRest delAtteInfoRest : attendeeRequest.getDelAtteInfoRest())
        {
            // 是否注册必须为0或1
            if (!"0".equals(delAtteInfoRest.getIsRegister()) && !"1".equals(delAtteInfoRest.getIsRegister()))
            {
                response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
                response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
                LoggerMgr.interfacelogger("POST", "delAttendee",
                    attendeeRequest, response.getResultCode());
                return response;
            }
        }
        
        try
        {
            SDKResult<AttendeeInfo> result =
                ATTENDEE_SERVICE.delAttendee(ATTENDEE_RESOURCE_CONVERT.delAttendeeRest2Model(attendeeRequest));
            
            response.setResultCode(String.valueOf(result.getErrCode()));
            response.setResultContext(StringUtils.avoidNull(result.getDescription()));
            response.setAttendeeInfo(result.getResult());
        }
        catch (SDKException e)
        {
            LOGGER.error("delAttendee method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultContext(e.getSdkErrDesc());
        }
        catch (Exception e)
        {
            LOGGER.error("delAttendee method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("POST", "delAttendee",
            attendeeRequest, response.getResultCode());
        
        return response;
    }
}
